package com.example.cms.until;


import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;

public class UploadFileUtil {

    /*
     * @Author Sunbird
     * @Description 文件下载
     * @Date  2018/11/21
     * @Param
     * @return
     **/

    public static void downFile(HttpServletResponse response , InputStream stream ,String fileName) throws  Exception{
        response.reset();
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition","attachement;filename="+new String(fileName.getBytes(),"iso-8859-1"));
        ServletOutputStream out =response.getOutputStream();
        BufferedInputStream bis =null;
        BufferedOutputStream bos =null;
        try {
            bis =new BufferedInputStream(stream);
            bos =new BufferedOutputStream(out);
            byte[] buff =new byte[2048];
            int size=0;
            while ((size=bis.read(buff))>-1){
                bos.write(buff,0,size);
            }
        }catch (final  Exception e){
            throw e;
        }finally {
            if (bis!=null){
                bis.close();
            }
            if (bos!=null){
                bos.close();
            }
            if (stream!=null){
                stream.close();
            }
        }
        
    }
    /**
     * @Author Sunbird
     * @Description 文件下载
     * @Date  2018/11/21
     * @Param 
     * @return 
     **/
    public static void dowload(HttpServletResponse response, String file ,String fileName)throws  Exception{
        InputStream inputStream =new FileInputStream(file);
        downFile(response,inputStream,fileName);
    }
    public static void downFile(HttpServletResponse response, String file ,String fileName)throws  Exception{
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition","attachement;filename="+new String(fileName.getBytes(),"iso-8859-1"));
        InputStream inputStream =new FileInputStream(file);
        downFile(response,inputStream,fileName);
    }
}
